package com.lindroy.androidplayer.ui.coin

import android.widget.ImageView
import androidx.activity.viewModels
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseActivity
import com.lindroy.androidplayer.extensions.BaseSimpleAdapter
import com.lindroy.androidplayer.extensions.initColor
import com.lindroy.androidplayer.extensions.loadListData
import com.lindroy.androidplayer.extensions.setLineDivider
import com.lindroy.androidplayer.logic.model.CoinRankingResp
import com.lindroy.androidplayer.viewmodel.CoinViewModel
import com.lindroy.lib_utils.extensions.setVisible
import com.youngfeng.snake.annotations.EnableDragToClose
import kotlinx.android.synthetic.main.activity_coin_rank.*
import kotlinx.android.synthetic.main.activity_coin_rank.refreshHeader
import kotlinx.android.synthetic.main.activity_coin_rank.refreshLayout
import kotlinx.android.synthetic.main.activity_coin_rank.stateLayout


/**
 * @author Lin
 * @date 2021/4/27
 * @function 积分排行榜
 * @Description
 */
@EnableDragToClose
class CoinRankActivity(override val layoutId: Int = R.layout.activity_coin_rank) : BaseActivity() {

    private val coinViewModel: CoinViewModel by viewModels()

    private val adapter by lazy {
        BaseSimpleAdapter<CoinRankingResp>(R.layout.coin_rank_item) { holder, item ->
            holder.setText(R.id.tvName, item.username)
                .setText(R.id.tvCoin, item.coinCount.toString())
                .setText(R.id.tvRankNum, item.rank)
            when (item.rank) {
                "1" -> {
                    holder.setImageResource(R.id.ivRank, R.drawable.ic_rank1)
                        .setGone(R.id.ivRank, false)
                }
                "2" -> {
                    holder.setImageResource(R.id.ivRank, R.drawable.ic_rank2)
                        .setGone(R.id.ivRank, false)
                }
                "3" -> {
                    holder.setImageResource(R.id.ivRank, R.drawable.ic_rank3)
                        .setGone(R.id.ivRank, false)
                }
                else -> {
                    holder.setGone(R.id.ivRank, true)
                }
            }
        }
    }

    override fun initView() {
        initToolBar(R.string.coin_ranking)
        refreshHeader.initColor()
        rvRanking.setLineDivider()
        rvRanking.adapter = adapter
    }

    override fun initData() {
        reqCoinRanking(true)
    }

    override fun initListener() {
        coinViewModel.coinRankingState.observe(this) {
            loadListData(it, adapter, refreshLayout, stateLayout)
        }
        refreshLayout.setOnRefreshListener { reqCoinRanking(false) }
        refreshLayout.setOnLoadMoreListener { reqCoinRanking(false, false) }
        stateLayout.setOnViewsClickListener { state, view ->
            reqCoinRanking(true)
        }
    }

    private fun reqCoinRanking(showLoading: Boolean, isRefresh: Boolean = true) {
        if (showLoading) {
            stateLayout.showLoading()
        }
        coinViewModel.reqCoinRanking(isRefresh)
    }

    override fun initClick() {
    }
}